'Brendan's Functions
' $HeadURL: http://gabbscript.googlecode.com/svn/branches/funcbranch/Gabbscript/BrendanFunctions.vb $
' $Author: iwannalightsaber@hotmail.com $
' $Revision: 84 $
' $Date: 2007-11-06 19:47:43 +0000 (Tue, 06 Nov 2007) $

Module BrendanFunctions
    Sub RegisterFunctions(ByRef funcList As FunctionList)
        funcList.RegisterFunction("write", AddressOf consolewrite)
        funcList.RegisterFunction("read", AddressOf consoleread)
        funcList.RegisterFunction("add", AddressOf consoleadd)
        funcList.RegisterFunction("rnd", AddressOf consolernd)
        funcList.RegisterFunction("sub", AddressOf consolesubtract)
        funcList.RegisterFunction("goto", AddressOf consolegoto)
        funcList.RegisterFunction("mul", AddressOf consolemultiply)
        funcList.RegisterFunction("div", AddressOf consoledivide)
        funcList.RegisterFunction("floor", AddressOf consolefloor)
        funcList.RegisterFunction("ceil", AddressOf consoleceil)
        funcList.RegisterFunction("exp", AddressOf consoleexp)
        funcList.RegisterFunction("root", AddressOf consoleroot)
        funcList.RegisterFunction("val", AddressOf consoleval)
        funcList.RegisterFunction("time", AddressOf consoletime)
    End Sub
    Function consolewrite(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False 'do not run function if less than 1 argument
        Console.WriteLine(args.Item(0).Data) 'write the data from argument 0 to the screen
        Return True 'return function as executed
    End Function
    Function consoleread(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False ' do no run function if less than 1 argument
        args.Item(0).Data = Console.ReadLine() 'reads the line that is typed in argument 0
        Return True 'return function as executed
    End Function
    Function consoleadd(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'do not run function if less than 3 arguments
        args.Item(2).Data = (args.Item(0) + args.Item(1)).Data 'argument 2 is arguments 1 and 2 added together
        Return True 'return function as executed
    End Function
    Function consolernd(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'do not run function if less than 3 arguments
        Randomize() 'randomize and set arguments 1 and 2 to min/max variables
        Dim max As Integer = args.Item(1).Data
        Dim min As Integer = args.Item(0).Data
        args.Item(2).Data = ((max - min + 1) * Rnd() + min) 'argument 2 is a random number generated using the min/max
        Return True 'return function as executed
    End Function
    Function consolesubtract(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run function
        args.Item(2).Data = args.Item(0) - args.Item(1) 'argument 2 is argument 0 minus argument 1
        Return True 'return function as executed
    End Function
    Function consolegoto(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False 'if less than 1 function do not run
        script.SetInstructionPointer(args.Item(0).Data) ' set instruction pointer to line in argument 0
        Return True ' return function as executed
    End Function
    Function consolemultiply(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False ' if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0).Data * args.Item(1).Data 'argument 2 equals argument 0 times argument 1
        Return True 'return function as executed
    End Function
    Function consoledivide(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0).Data / args.Item(1).Data 'argument 2 equals argument 0 divided by argument 1
        Return True 'return function as executed
    End Function
    Function consolefloor(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False 'if less than 1 argument do not run
        Math.Floor(args.Item(0).Data)       'get rid of decimal on number
        Return True 'return function as executed
    End Function
    Function consoleceil(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return True 'if less than 1 argument do not run
        Math.Ceiling(args.Item(0).Data) 'round number up
        Return True 'return function as executed
    End Function
    Function consoleexp(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0).Data ^ args.Item(1).Data   'argument 2 equals argument 0 to the power of argument 1
        Return True 'return function as executed
    End Function
    Function consoleroot(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0) ^ (1 / args.Item(1).Data)  'root argument 0 from argument 1
        Return True 'return as executed
    End Function
    Function consoleval(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 2 Then Return False
        args.Item(1).Data = Val(args.Item(0))
        Return True
    End Function
    Function consoletime(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False
        Dim currenttime As Date
        currenttime = TimeOfDay
        args.Item(0).Data = currenttime.ToString()
    End Function
End Module  'finish
